package com.ruoyi.coal.modules.coalpile.controller;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.coal.common.annotation.SysLog;
import com.ruoyi.coal.common.utils.PageUtils;
import com.ruoyi.coal.common.utils.R;
import com.ruoyi.coal.modules.coalpile.domain.CoalPileInfoEntity;
import com.ruoyi.coal.modules.coalpile.domain.CoalYardHistoryEntity;
import com.ruoyi.coal.modules.coalpile.domain.CoalYardInfoEntity;
import com.ruoyi.coal.modules.coalpile.domain.PointEntity;
import com.ruoyi.coal.modules.coalpile.domain.vo.CoalPileInfoVo;
import com.ruoyi.coal.modules.coalpile.mapper.CoalYardHistoryDao;
import com.ruoyi.coal.modules.coalpile.service.CoalPileInfoService;
import com.ruoyi.coal.modules.coalpile.service.CoalYardHistoryService;
import com.ruoyi.coal.modules.coalpile.service.CoalYardInfoService;
import com.ruoyi.coal.modules.coalpile.service.PointService;
import com.ruoyi.coal.modules.coalpile.utils.compute.Compute;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@Api(value = "coalpileinfo", tags = {"煤堆数据信息"})
@RestController
@RequestMapping({"coalpile/coalpileinfo"})
@DataSource(value = DataSourceType.SLAVE)
public class CoalPileInfoController {
    @Autowired
    private CoalYardInfoService coalYardInfoService;

    @Autowired
    private CoalPileInfoService coalPileInfoService;

    @Autowired
    private PointService pointService;

    @Autowired
    private CoalYardHistoryDao coalYardHistoryDao;

    @Autowired
    private CoalYardHistoryService coalYardHistoryService;

    @ApiOperation(value = "分页查询", notes = "分页查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"), @ApiImplicitParam(name = "limit", value = "每页显示记录数", paramType = "query", required = true, dataType = "int"), @ApiImplicitParam(name = "sidx", value = "排序字段", paramType = "query", dataType = "String"), @ApiImplicitParam(name = "order", value = "排序方式，可选值(asc,desc)", paramType = "query", dataType = "String"), @ApiImplicitParam(name = "key", value = "关键字", paramType = "query", dataType = "String")})
    @GetMapping({"/list"})
    @RequiresPermissions({"coalpile:coalpileinfo:list"})
    public R list(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageUtils page = this.coalPileInfoService.queryPage(params);
        for (Object o : page.getList()) {
            CoalPileInfoEntity coalPileInfoEntity = (CoalPileInfoEntity) o;
            CoalYardHistoryEntity coalYardHistoryEntity = this.coalYardHistoryDao.selectById(coalPileInfoEntity.getHistoryId());
            if(coalYardHistoryEntity == null)continue;
            CoalYardInfoEntity byId = (CoalYardInfoEntity) this.coalYardInfoService.getById(((CoalYardHistoryEntity) this.coalYardHistoryDao.selectById(coalPileInfoEntity.getHistoryId())).getCoalYardId());
            if(byId == null)continue;
            List<CoalPileInfoVo> pdfVo1 = this.coalPileInfoService.getALLByHistoryId(coalPileInfoEntity.getHistoryId());
            if(pdfVo1 == null)continue;
            if (byId.getName().equals(coalPileInfoEntity.getName()) && pdfVo1.size() > 1) {
                CoalPileInfoVo lastPdfVo = pdfVo1.get(pdfVo1.size() - 1);
                double sumOfVolumes = pdfVo1.stream().limit((pdfVo1.size() - 1)).mapToDouble(CoalPileInfoVo::getVolume).sum();
                double sumOfWeights = pdfVo1.stream().limit((pdfVo1.size() - 1)).mapToDouble(CoalPileInfoVo::getWeight).sum();
                coalPileInfoEntity.setVolume(Double.valueOf(Double.parseDouble(NumberUtil.roundStr(sumOfVolumes, 3))));
                coalPileInfoEntity.setWeight(Double.valueOf(Double.parseDouble(NumberUtil.roundStr(sumOfWeights, 3))));
                this.coalPileInfoService.updateById(coalPileInfoEntity);
            }
        }
        return R.ok().put("page", page);
    }

    @ApiOperation(value = "通过id查询", notes = "通过id查")
    @GetMapping({"/info/{id}"})
    @RequiresPermissions({"coalpile:coalpileinfo:info"})
    public R info(@PathVariable("id") Integer id) {
        CoalPileInfoEntity coalPileInfo = (CoalPileInfoEntity) this.coalPileInfoService.getById(id);
        return R.ok().put("coalPileInfo", coalPileInfo);
    }

    @ApiOperation(value = "通过煤场id查询所有最新煤堆信息", notes = "通过煤场id查询所有最新煤堆信息")
    @GetMapping({"/getALL/{coalYardId}"})
    @SysLog("查询所有最新煤堆信息")
    public R getALLByCoalYardId(@PathVariable("coalYardId") Integer coalYardId) {
        List<CoalPileInfoVo> coalPileInfo = this.coalPileInfoService.getALLByCoalYardId(coalYardId);
        return R.ok().put("coalPileInfo", coalPileInfo);
    }

    @ApiOperation(value = "通过煤场id和煤堆id查询所有最新煤堆信息", notes = "通过煤场id和煤堆id查询所有最新煤堆信息")
    @GetMapping({"/getALL"})
    public R getOneByCoalYardIdAndPileId(@RequestParam("coalYardId") Integer coalYardId, @RequestParam("coalPileId") Integer coalPileId) {
        CoalPileInfoVo coalPileInfo = this.coalPileInfoService.getOneByCoalYardIdAndCoalPileId(coalYardId, coalPileId);
        return R.ok().put("singleCoalPileInfo", coalPileInfo);
    }

    @ApiOperation(value = "过和煤场名字同名的煤堆查询最新煤堆信息", notes = "过和煤场名字同名的煤堆查询最新煤堆信息")
    @GetMapping({"/getCoalPileByName"})
    public R getOneByCoalYardIdAndPileId(@RequestParam("coalYardName") String coalYardName) {
        CoalPileInfoEntity coalPileInfo = (CoalPileInfoEntity) this.coalPileInfoService.getOne((Wrapper) ((QueryWrapper) ((QueryWrapper) ((QueryWrapper) (new QueryWrapper()).eq("name", coalYardName)).eq("measure_type", Integer.valueOf(1))).orderBy(true, false, (Object[]) new String[]{"create_time"})).last("limit 0,1"));
        if (coalPileInfo == null) {
            CoalPileInfoVo coalPileInfoVo1 = new CoalPileInfoVo();
            return R.ok().put("singleCoalPileInfo", coalPileInfoVo1);
        }
        CoalYardInfoEntity byId = (CoalYardInfoEntity) this.coalYardInfoService.getById(((CoalYardHistoryEntity) this.coalYardHistoryDao.selectById(coalPileInfo.getHistoryId())).getCoalYardId());
        List<CoalPileInfoVo> pdfVo1 = this.coalPileInfoService.getALLByHistoryId(coalPileInfo.getHistoryId());
        Map<String, Double> stringDoubleMap = Compute.computeWholeCoalpile(byId.getName(), pdfVo1);
        coalPileInfo.setVolume(stringDoubleMap.get("sumVolume"));
        coalPileInfo.setWeight(stringDoubleMap.get("sumWeight"));
        CoalPileInfoVo coalPileInfoVo = new CoalPileInfoVo();
        BeanUtils.copyProperties(coalPileInfo, coalPileInfoVo);
        coalPileInfoVo.setMaxPoint((PointEntity) this.pointService.getById(coalPileInfo.getMaxPoint()));
        coalPileInfoVo.setMinPoint((PointEntity) this.pointService.getById(coalPileInfo.getMinPoint()));
        return R.ok().put("singleCoalPileInfo", coalPileInfoVo);
    }

    @ApiOperation(value = "过和煤场名字同名的煤堆查询最新煤堆信息", notes = "过和煤场名字同名的煤堆查询最新煤堆信息")
    @GetMapping({"/getCoalPileByNameSecond"})
    public R getOneByCoalYardIdAndPileIdSecond(@RequestParam("coalYardName") String coalYardName) {
        CoalPileInfoEntity coalPileInfo = (CoalPileInfoEntity) this.coalPileInfoService.getOne((Wrapper) ((QueryWrapper) ((QueryWrapper) ((QueryWrapper) (new QueryWrapper()).eq("name", coalYardName)).eq("measure_type", Integer.valueOf(2))).orderBy(true, false, (Object[]) new String[]{"create_time"})).last("limit 0,1"));
        if (coalPileInfo == null) {
            CoalPileInfoVo coalPileInfoVo1 = new CoalPileInfoVo();
            return R.ok().put("singleCoalPileInfo", coalPileInfoVo1);
        }
        CoalYardInfoEntity byId = (CoalYardInfoEntity) this.coalYardInfoService.getById(((CoalYardHistoryEntity) this.coalYardHistoryDao.selectById(coalPileInfo.getHistoryId())).getCoalYardId());
        List<CoalPileInfoVo> pdfVo1 = this.coalPileInfoService.getALLByHistoryId(coalPileInfo.getHistoryId());
        Map<String, Double> stringDoubleMap = Compute.computeWholeCoalpile(byId.getName(), pdfVo1);
        coalPileInfo.setVolume(stringDoubleMap.get("sumVolume"));
        coalPileInfo.setWeight(stringDoubleMap.get("sumWeight"));
        CoalPileInfoVo coalPileInfoVo = new CoalPileInfoVo();
        BeanUtils.copyProperties(coalPileInfo, coalPileInfoVo);
        coalPileInfoVo.setMaxPoint((PointEntity) this.pointService.getById(coalPileInfo.getMaxPoint()));
        coalPileInfoVo.setMinPoint((PointEntity) this.pointService.getById(coalPileInfo.getMinPoint()));
        return R.ok().put("singleCoalPileInfo", coalPileInfoVo);
    }

    @ApiOperation(value = "新增煤堆数据信息", notes = "新增煤堆数据信息")
    @PostMapping({"/save"})
    @RequiresPermissions({"coalpile:coalpileinfo:save"})
    public R save(@RequestBody CoalPileInfoEntity coalPileInfo) {
        this.coalPileInfoService.save(coalPileInfo);
        return R.ok();
    }

    @ApiOperation(value = "根据id修改煤堆数据信息", notes = "根据id修改煤堆数据信息")
    @PostMapping({"/update"})
    @RequiresPermissions({"coalpile:coalpileinfo:update"})
    public R update(@RequestBody CoalPileInfoEntity coalPileInfo) {
        coalPileInfo.setWeight(Double.valueOf(coalPileInfo.getVolume().doubleValue() * coalPileInfo.getDensity().doubleValue()));
        this.coalPileInfoService.updateById(coalPileInfo);
        return R.ok();
    }

    @ApiOperation(value = "根据id删除煤堆数据信息", notes = "根据id删除煤堆数据信息")
    @PostMapping({"/delete"})
    @RequiresPermissions({"coalpile:coalpileinfo:delete"})
    public R delete(@RequestBody Integer[] ids) {
        this.coalPileInfoService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    @ApiOperation(value = "查询除主煤场的分堆信息", notes = "查询除主煤场的分堆信息")
    @GetMapping({"/getCoalPileExceptMain"})
    public R getCoalPileExceptMain(@RequestParam("coalYardId") Integer coalYardId) {
        List<CoalPileInfoVo> coalPileInfo = this.coalPileInfoService.getCoalPileExceptMain(coalYardId);
        return R.ok().put("coalPileInfo", coalPileInfo);
    }

    @GetMapping({"/getCoalPileGltf/{coalYardId}"})
    public R getCoalPileGltf(@PathVariable("coalYardId") Integer coalYardId) {
        List<CoalPileInfoVo> coalPileInfo = this.coalPileInfoService.getCoalPileGltf(coalYardId);
        if (coalPileInfo.size() == 0)
            return R.error("该煤场没有扫描数据");
        CoalYardHistoryEntity coalYardHistoryEntity = (CoalYardHistoryEntity) this.coalYardHistoryService.getOne((Wrapper) ((QueryWrapper) ((QueryWrapper) (new QueryWrapper()).eq("coal_yard_id", coalYardId)).orderByDesc("time")).last("limit 1"));
        for (CoalPileInfoVo coalPileInfoVo : coalPileInfo)
            coalPileInfoVo.setUrlPcd(coalYardHistoryEntity.getUrl());
        return R.ok().put("coalPileInfo", coalPileInfo);
    }
}

